package com.ruoyi.basicsystem.controller;

import com.ruoyi.basicsystem.domain.*;
import com.ruoyi.basicsystem.service.IZnAccountItemService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basicsystem.service.IZnAccountHeadService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 财务主Controller
 * 
 * @author ruoyi
 * @date 2022-02-23
 */
@RestController
@RequestMapping("/basicsystem/accounthead")
public class ZnAccountHeadController extends BaseController
{
    @Autowired
    private IZnAccountHeadService znAccountHeadService;
    @Autowired
    private IZnAccountItemService znAccountItemService;
    /**
     * 查询财务主列表
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:list,basicsystem:accountheadsr:list,basicsystem:accountheadzc:list,basicsystem:accountheadsk:list,basicsystem:accountheadfk:list,basicsystem:accountheadzz:list,basicsystem:accountheadyfk:list,basicsystem:accountheadqcyf:list,basicsystem:accountheadqcys:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnAccountHead znAccountHead)
    {
        startPage();
        znAccountHead.setTenantId(SecurityUtils.getTenantId());
        znAccountHead.setDeleteFlag("0");
        List<ZnAccountHead> list = znAccountHeadService.selectZnAccountHeadList(znAccountHead);
        return getDataTable(list);
    }

//    /**
//     * 导出财务主列表
//     */
//    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:export,basicsystem:accountheadsr:export,basicsystem:accountheadzc:export,basicsystem:accountheadsk:export,basicsystem:accountheadfk:export,basicsystem:accountheadzz:export,basicsystem:accountheadyfk:export')")
//    @Log(title = "财务主", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ZnAccountHead znAccountHead)
//    {
//        znAccountHead.setTenantId(SecurityUtils.getTenantId());
//        List<ZnAccountHead> list = znAccountHeadService.selectZnAccountHeadList(znAccountHead);
//        ExcelUtil<ZnAccountHead> util = new ExcelUtil<ZnAccountHead>(ZnAccountHead.class);
//        util.exportExcel(response, list, "财务主数据");
//    }

    /**
     * 获取财务主详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:query,basicsystem:accountheadsr:query,basicsystem:accountheadzc:query,basicsystem:accountheadsk:query,basicsystem:accountheadfk:query,basicsystem:accountheadzz:query,basicsystem:accountheadyfk:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(znAccountHeadService.selectZnAccountHeadCulItemById(id));
    }

    /**
     * 新增财务主
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:add,basicsystem:accountheadsr:add,basicsystem:accountheadzc:add,basicsystem:accountheadsk:add,basicsystem:accountheadfk:add,basicsystem:accountheadzz:add,basicsystem:accountheadyfk:add')")
    @Log(title = "财务主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnAccountHead znAccountHead)
    {
        int  rows= znAccountHeadService.insertZnAccountHead(znAccountHead);
        AjaxResult ajaxResult = toAjax(rows);
        if (rows==-1){
            ajaxResult.put("msg","收款金额大于可客户原有期初应收");
        }else  if (rows==-2){
            ajaxResult.put("msg","付款金额大于可客户原有期初应付");
        }
        return ajaxResult;
    }

    /**
     * 修改财务主
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:edit,basicsystem:accountheadsr:edit,basicsystem:accountheadzc:edit,basicsystem:accountheadsk:edit,basicsystem:accountheadfk:edit,basicsystem:accountheadzz:edit,basicsystem:accountheadyfk:edit')")
    @Log(title = "财务主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnAccountHead znAccountHead)
    {
//        return toAjax(znAccountHeadService.updateZnAccountHead(znAccountHead));
        int  rows= znAccountHeadService.updateZnAccountHead(znAccountHead);
        AjaxResult ajaxResult = toAjax(rows);
        if (rows==-1){
            ajaxResult.put("msg","收款金额大于可客户原有期初应收");
        }else  if (rows==-2){
            ajaxResult.put("msg","付款金额大于可客户原有期初应付");
        }
        return ajaxResult;
    }

    /**
     * 删除财务主
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:remove,basicsystem:accountheadsr:remove,basicsystem:accountheadzc:remove,basicsystem:accountheadsk:remove,basicsystem:accountheadfk:remove,basicsystem:accountheadzz:remove,basicsystem:accountheadyfk:remove')")
    @Log(title = "财务主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(znAccountHeadService.deleteZnAccountHeadByIds(ids));
    }
    /**
     * 删除财务主
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:remove,basicsystem:accountheadsr:remove,basicsystem:accountheadzc:remove,basicsystem:accountheadsk:remove,basicsystem:accountheadfk:remove,basicsystem:accountheadzz:remove,basicsystem:accountheadyfk:remove')")
    @Log(title = "财务主", businessType = BusinessType.DELETE)
    @DeleteMapping("/qc/{ids}")
    public AjaxResult removeQC(@PathVariable Long[] ids)
    {
        return toAjax(znAccountHeadService.deleteZnAccountHeadQcByIds(ids));
    }
    /**
     * 修改单据信息状态
     * @param status 状态 1 审核 0 反审核
     * @param ids 单据主键
     * @author nexiaozi
     * @date 2022年2月20日
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:check,basicsystem:accountheadsr:check,basicsystem:accountheadzc:check,basicsystem:accountheadsk:check,basicsystem:accountheadfk:check,basicsystem:accountheadzz:check,basicsystem:accountheadyfk:check,basicsystem:accountheadsr:unCheck,basicsystem:accountheadzc:unCheck,basicsystem:accountheadsk:unCheck,basicsystem:accountheadfk:unCheck,basicsystem:accountheadzz:unCheck,basicsystem:accountheadyfk:unCheck')")
    @Log(title = "单据管理", businessType = BusinessType.UPDATE)
    @GetMapping("/{status}/{ids}")
    public AjaxResult changeStatus(@PathVariable("status") String status,@PathVariable("ids") Long[] ids )
    {
        int rows = znAccountHeadService.changeStatus(ids,status);
//        if(rows < ids.length){
//            return AjaxResult.success("单据" + (status.equals("1")?"审核":"反审核") + "成功,有部分单据已"+(status.equals("1")?"审核":"反审核"));
//        }else if(rows ==0){//抱歉，只有未审核的单据才能审核
//            return AjaxResult.error("抱歉，只有"+(status.equals("1")?"未审核":"已审核")+"的单据才能" + (status.equals("1")?"审核":"反审核"));
//        }else{
//            return AjaxResult.success("单据" + (status.equals("1")?"审核":"反审核") + "成功");
//        }
        if(rows ==0){//抱歉，只有未审核的单据才能审核
            return AjaxResult.error("抱歉，只有"+(status.equals("1")?"未审核":"已审核")+"的单据才能" + (status.equals("1")?"审核":"反审核"));
        }else{
            if(rows < ids.length){//部分成功
                return AjaxResult.error("单据" + (status.equals("1")?"审核":"反审核") + "成功,有部分单据已"+(status.equals("1")?"审核":"反审核"));
            }else{//全部成功
                return AjaxResult.success("单据" + (status.equals("1")?"审核":"反审核") + "成功");
            }
        }
    }
    /**
     * 根据编号查询账户单据信息
     * @param
     * @param request
     * @return
     */
    @GetMapping(value = "/getDetailByNumber")
    public AjaxResult getDetailByNumber(@RequestParam("billNo") String billNo,
                                        HttpServletRequest request)throws Exception {
//        AjaxResult res = new AjaxResult();
        AccountHeadVo4ListEx ahl = new AccountHeadVo4ListEx();
        List<AccountHeadVo4ListEx> list = znAccountHeadService.getDetailByNumber(billNo);
        if(list.size() == 1) {
            ahl = list.get(0);
        }
//        res.put("data",ahl);
        return AjaxResult.success(ahl);
    }
    /**
     * 单据明细列表
     * @param znAccountItem
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getDetailList")
    public AjaxResult getDetailList(ZnAccountItem znAccountItem) {
        znAccountItem.setTenantId(SecurityUtils.getTenantId());
        znAccountItem.setDeleteFlag("0");
        List<ZnAccountItem> list = znAccountItemService.selectZnAccountItemList(znAccountItem);
        return AjaxResult.success(list);
    }
    /**
     * 获取单据编号
     * @param  type 0收入[SR]、1支出[ZC]) 2收款[SK] 3付款[FK] 4转账[zz] 5[YF]
     * @author nexiaozi
     * @date 2022年2月20日
     */
    @GetMapping(value = "/getAccountNo/{type}")
    public AjaxResult getAccountNo(@PathVariable("type") String type)
    {
        return AjaxResult.success("单据编号生成成功",znAccountHeadService.getAccountNo(type.trim()));
    }

    /**
     * 财务导出
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('basicsystem:accounthead:export,basicsystem:accountheadsr:export,basicsystem:accountheadzc:export,basicsystem:accountheadsk:export,basicsystem:accountheadfk:export,basicsystem:accountheadzz:export,basicsystem:accountheadyfk:export')")
    @Log(title = "财务管理导出", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/export")
    public void export(HttpServletResponse response, HttpServletRequest request,ZnAccountHead znAccountHead) throws Exception{
        znAccountHead.setTenantId(SecurityUtils.getTenantId());
        znAccountHead.setDeleteFlag("0");
        List<ZnAccountHeadExportVo> list = znAccountHeadService.selectZnAccountHeadExportList(znAccountHead);
        List result= new ArrayList<>();
        if(znAccountHead.getType().equals("收入")){
                    ExcelUtil<ZnAccountHeadSrExportVo> util = new ExcelUtil<ZnAccountHeadSrExportVo>(ZnAccountHeadSrExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadSrExportVo vo = new ZnAccountHeadSrExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else if(znAccountHead.getType().equals("支出")){
            ExcelUtil<ZnAccountHeadZcExportVo> util = new ExcelUtil<ZnAccountHeadZcExportVo>(ZnAccountHeadZcExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadZcExportVo vo = new ZnAccountHeadZcExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else if(znAccountHead.getType().equals("收款")){
            ExcelUtil<ZnAccountHeadSkExportVo> util = new ExcelUtil<ZnAccountHeadSkExportVo>(ZnAccountHeadSkExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadSkExportVo vo = new ZnAccountHeadSkExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else if(znAccountHead.getType().equals("付款")){
            ExcelUtil<ZnAccountHeadFkExportVo> util = new ExcelUtil<ZnAccountHeadFkExportVo>(ZnAccountHeadFkExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadFkExportVo vo = new ZnAccountHeadFkExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else if(znAccountHead.getType().equals("转账")){
            ExcelUtil<ZnAccountHeadZzExportVo> util = new ExcelUtil<ZnAccountHeadZzExportVo>(ZnAccountHeadZzExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadZzExportVo vo = new ZnAccountHeadZzExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else if(znAccountHead.getType().equals("期初应付") || znAccountHead.getType().equals("期初应收")){
            ExcelUtil<ZnAccountHeadQcExportVo> util = new ExcelUtil<ZnAccountHeadQcExportVo>(ZnAccountHeadQcExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadQcExportVo vo = new ZnAccountHeadQcExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }else {
            ExcelUtil<ZnAccountHeadYfkExportVo> util = new ExcelUtil<ZnAccountHeadYfkExportVo>(ZnAccountHeadYfkExportVo.class);
            for (ZnAccountHeadExportVo temp:list) {
                ZnAccountHeadYfkExportVo vo = new ZnAccountHeadYfkExportVo();
                BeanUtils.copyProperties(temp,vo);
                result.add(vo);
            }
            util.exportExcel(response, result, znAccountHead.getType());
        }
    }

}
